import axios from 'axios';
import { getToken } from '../utils/auth';
const instance = axios.create({
        baseURL: 'http://localhost:3009',
        //timeout的作用其实就是，设置了在5000毫秒内请求数据  如果没有请求成功就执行错误函数
        timeout: 5000,
    })
    // Add a request interceptor
instance.interceptors.request.use(function(config) {
    // config.headers['authorization']  = 'Bearer ' + getToken();
    // return config;
    config.headers["authorization"] = "Bearer " + getToken();
    return config;
}, function(error) {
    // Do something with request error
    return Promise.reject(error);
});

// Add a response interceptor
instance.interceptors.response.use(function(response) {

    return response.data;
}, function(error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error);
});









// ==========================================================================================
//get请求
export function get(url, param) {
    return instance.get(url, param)
}
//post请求
export function post(url, param) {
    return instance.post(url, param);
}
//put请求
export function put(url, data) {
    return instance.put(url, data)
}
//delete请求
export function del(url, param) {
    return instance.delete(url, param);
}